<template>
  <div class="login">
    <div class="box">
      <div class="title">Odin7CJ - 系统登录</div>
      <!-- <el-divider></el-divider> -->
      <div class="form">
        <el-form  :model="loginForm" :rules="loginRules" ref="loginForm" class="demo-ruleForm">
          <el-form-item prop="username">
            <el-input
              @keyup.enter.native="submitForm('loginForm')"
              v-model="loginForm.username"
              placeholder="账号"
            >
              <i slot="prefix" class="iconfont iconuser-fill"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              @keyup.enter.native="submitForm('loginForm')"
              :type="passwordType"
              v-model="loginForm.password"
              placeholder="密码"
            >
              <i slot="prefix" class="iconfont iconlock-fill"></i>
              <i
                slot="suffix"
                @click="showPwd"
                :class=" passwordType=='password'? 'iconfont iconeyeslash-fill':'iconfont iconeye-fill'"
              ></i>
            </el-input>
          </el-form-item>
          <div class="btns">
            <el-button
              type="primary"
              :loading="btnload"
              @click="submitForm('loginForm')"
            >登录</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { loginIn, disconnect, mustChangePass } from "./function.js";
export default {
  components: {},
  data() {
    return {
      passwordType: "password",
      btnload: false,
      loginForm: {
        username: null,
        password: null
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" }
        ]
      }
    };
  },
  computed: {},
  mounted() {},
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.btnload = true;
          let params = {
            username: this.loginForm.username,
            password: this.$md5(this.loginForm.password)
          };
          loginIn(params)
            .then(res => {
              this.btnload = false;
              console.log(res);
              if (res.retHead.code == "0000000" && res.retBody) {
                this.$message.success("登录成功！");
                let user = JSON.stringify(res.retBody);
                sessionStorage.setItem("token", res.retBody.token);
                sessionStorage.setItem("user", user);
                this.$router.push("/");

                // window.Refresh是刷新
                // add by zhujq 该用户关闭已存在的websocket连接
                // disconnect(user.id);

                /**
                mustChangePass({ id: res.retBody.id }).then(res => {
                   const h = this.$createElement;
                  if (res.retHead.code == "0000000" && res.retBody) {
                    let myNotify =this.$notify({
                      // title:'温馨提示：'+ res.retBody,
                      title: res.retBody,
                      type: "warning",
                      dangerouslyUseHTMLString: true,
                      message: h(
                        "i",
                        {
                          style: "color: teal",
                          on: {
                            click: () => {
                              this.$router.push("/user/mod-password")
                              myNotify.close();
                            }
                          }
                        },
                        "立即前往"
                      ),
                      duration: 0
                    });
                  }
                });
                */

              } else {
                this.$message.error(res.retHead.message);
              }
            })
            .catch(error => {
              this.btnload = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang='scss' scoped>
$bgc: #2d3a4b;
$inputBg: #283443;
.el-form {
  .el-form-item__content .el-input {
    width: 100%!important;
  }
}
.login {
  width: 100%;
  height: 100%;
  background-color: $bgc;
  .box {
    width: 400px;
    height: 300px;
    // background-color: #fff;
    border-radius: 10px;
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .title {
      text-align: center;
      color: #fff;
      line-height: 60px;
      font-size: 30px;
      font-weight: 700;
    }
    .form {
      padding: 14px;
      /deep/ .el-input__inner {
        background-color: $inputBg;
        color: #fff;
      }
      .btns {
        text-align: center;
        margin-top: 40px;
        button {
          width: 100%;
        }
      }
    }
  }
}
/deep/.el-divider--horizontal {
  margin: 0;
}
/deep/.my-form-item.is-error .el-input__inner,
/deep/.el-input__inner:focus {
  border: 1px solid #dcdfe6;
}
</style>